package com.chapter.sample.web;

import com.chapter.sample.config.SpringConfig;
import com.chapter.sample.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/accountServlet")
public class AccountServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        ApplicationContext applicationContext = (ApplicationContext) req.getServletContext().getAttribute("applicationContext");
        AccountService bean = applicationContext.getBean(AccountService.class);
        bean.transferMoney("tom","lucy",500);
        //super.doGet(req, resp);
    }
}
